/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package algorithm;

import java.util.ArrayList;

/**
 *
 * @author Lu
 */
public class Checker {

    private ArrayList<ArrayList<WorkingTask>> solution;
    private ArrayList<WorkingTask> tasksRunning;
    private int[] currentIndexByRessources;
    private ProblemData data;

    public Checker(ProblemData data, ArrayList<ArrayList<WorkingTask>> solution) {
        this.data = data;
        this.solution = solution;
        tasksRunning = new ArrayList<WorkingTask>();
        currentIndexByRessources = new int[solution.size()];
    }

    public boolean run() {
        int time = 0;
        boolean isOk = true, notOver = true;
        WorkingTask a, b, newTask, previousTask;

        while (isOk && notOver) {
            // We get all current Tasks
            tasksRunning.clear();
            for (int i = 0; i < solution.size() && isOk; i++) {
                if (currentIndexByRessources[i] >= solution.get(i).size()) {
                    newTask = solution.get(i).get(currentIndexByRessources[i]);
                    tasksRunning.add(newTask);
                    if (i > 0) {
                        previousTask = solution.get(i).get(currentIndexByRessources[i] - 1);

                        if (newTask.getStartTime() <= previousTask.getEndTime()) {
                            isOk = false;
                        }
                    }
                }

            }
            // We check every products
            for (int i = 0; i < tasksRunning.size() - 1 && isOk; i++) {
                time = Integer.MAX_VALUE;
                a = tasksRunning.get(i);
                for (int j = i + 1; j < tasksRunning.size() && isOk; j++) {
                    b = tasksRunning.get(j);
                    if (a.getMachineUsed() == b.getMachineUsed()) {
                        isOk = false;
                    } else if (a.getProductUsed() == b.getProductUsed()) {
                        isOk = false;
                    } else if (a.getRessourceUsed() == b.getRessourceUsed()) {
                        isOk = false;
                        System.out.println("ERROR : 2 tasks had the same ressources, should be impossible");
                    }
                }
                // We check if it is the shortest time and get it as our next loop time
                if (a.getEndTime() + 1 < time) {
                    time = a.getEndTime() + 1;
                }
            }
            // We set the new currentTasks
            for (int i = 0; i < tasksRunning.size() && isOk; i++) {
                if (tasksRunning.get(i).getEndTime() < time) {
                    currentIndexByRessources[i]++;
                }
            }
            // If there is no more running tasks, it's over
            if (tasksRunning.size() == 0) {
                notOver = false;
            }
        }
        return isOk;
    }
}
